package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.models.DeptSalesRpt;
import com.moleq.models.GlobalInfo;
import com.moleq.models.GroupSalesRpt;

public class GroupSalesRptService {
	private DatabaseHelper DBHelper;
	private MessageDialog mMessageDialog;
	Context context;
	
	public GroupSalesRptService(){}
	
	public GroupSalesRptService(Context context){
		this.context = context;
		this.DBHelper = new DatabaseHelper(context);
	}
	
	public void DeleteAll() {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.GroupSalesRpt.SCHEMA_NAME);
	}
	
	public void DeleteByID(long id) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.GroupSalesRpt.SCHEMA_NAME
				+" WHERE "+ DatabaseSchema.GroupSalesRpt.GS_ID +" = ?",
				new Object[] { id });
	}
	
	public boolean CheckGroupInList(int group,String date){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = db.rawQuery("select gs_group from " + DatabaseSchema.GroupSalesRpt.SCHEMA_NAME 
				+ " where gs_group = "+ group +" and gs_date = '"+ date +"'", null);
		if (cursor == null) {
			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return false;
		}
		if (cursor.moveToNext()) {
			cursor.close();
			return true;
		}
		cursor.close();
		return false;
	}
	
	public void Update(GroupSalesRpt obj){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"Update " + DatabaseSchema.GroupSalesRpt.SCHEMA_NAME + " set gs_date = ?,gs_qty = gs_qty + ?," +
						"gs_amt = gs_amt + ?,update_datetime = strftime('%Y%m%d%H%M%S','now')" +
						" where gs_group = ? and gs_date = ?",
				new Object[] { obj.getGs_date(), obj.getGs_qty(), obj.getGs_amt(), obj.getGs_group(), obj.getGs_date() });
	}
	
	public List<GroupSalesRpt> FindAll(){
		List<GroupSalesRpt> obj = new ArrayList<GroupSalesRpt>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = db.rawQuery("select * from " + DatabaseSchema.GroupSalesRpt.SCHEMA_NAME, null);
		if (cursor == null) {
			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int gs_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.GroupSalesRpt.GS_ID));
			int gs_store_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.GroupSalesRpt.GS_STORE_ID));
			int gs_station = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.GroupSalesRpt.GS_STATION));
			String gs_date = cursor.getString(cursor.getColumnIndex(DatabaseSchema.GroupSalesRpt.GS_DATE));
			int gs_group = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.GroupSalesRpt.GS_GROUP));
			int gs_qty = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.GroupSalesRpt.GS_QTY));
			double gs_amt = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.GroupSalesRpt.GS_AMT));
			
			obj.add(new GroupSalesRpt(gs_id, gs_store_id, gs_station, gs_date, gs_group,
					gs_qty, gs_amt));
		}
		cursor.close();
		return obj;
	}
	
	public void Insert(GroupSalesRpt obj){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"INSERT INTO " + DatabaseSchema.GroupSalesRpt.SCHEMA_NAME + " VALUES(?,?,?,?,?,?,?," +
						"strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now'))",
				new Object[] { null, obj.getGs_id(), obj.getGs_station(), obj.getGs_date(), obj.getGs_group()
						, obj.getGs_qty(), obj.getGs_amt() });
	}
	
	public List<GroupSalesRpt> getGroupSalesRptByDate(String Date){
		List<GroupSalesRpt> list = new ArrayList<GroupSalesRpt>();
		String sql = "select * from t_group_sales_rpt where gs_date = '" + Date + "' order by gs_group";
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor == null) return list;
		while (cursor.moveToNext()) {
			GroupSalesRpt item = new GroupSalesRpt();
			item.setGs_id(cursor.getInt(0));
			item.setGs_store_id(cursor.getInt(1));
			item.setGs_station(cursor.getInt(2));
			item.setGs_date(cursor.getString(3));
			item.setGs_group(cursor.getInt(4));
			item.setGs_qty(cursor.getInt(5));
			item.setGs_amt(cursor.getDouble(6));
			list.add(item);
		}
		cursor.close();
		return list;
	}
	
	public String getDeptGroupName(String GrpId){
		String sql = "select dg_name from t_dept_group where dg_no = " + GrpId;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor == null) return "";
		if (cursor.moveToNext()) {
			String name = cursor.getString(0);
			cursor.close();
			return name;
		}
		return "";
	}
	
	public void ShowMessage(String text, int type, int icon) {
		mMessageDialog = new MessageDialog(context);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickOK() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}
	};

	public void save(GroupSalesRpt groupSalesRpt) {
		if (CheckGroupInList(groupSalesRpt.getGs_group(), groupSalesRpt.getGs_date())) {
			this.Update(groupSalesRpt);
		}else {
			this.Insert(groupSalesRpt);
		}
	}
}
